package com.bruip.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bruip.reggie.common.BaseResponse;
import com.bruip.reggie.common.CustomException;
import com.bruip.reggie.pojo.ShoppingCart;
import com.bruip.reggie.service.ShoppingCartService;
import com.bruip.reggie.util.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author 王若龙
 * @date 2022-10-11 22:17
 */
@RestController
@RequestMapping("shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 列表
     * todo
     *
     * @return {@link BaseResponse}<{@link List}<{@link ShoppingCart}>>
     */
    @GetMapping("/list")
    public BaseResponse<List<ShoppingCart>> list() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        return BaseResponse.success(shoppingCartList);
    }

    @PostMapping("/add")
    public BaseResponse<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        Long currentId = BaseContext.getCurrentId();
        if (currentId == null) {
            throw new CustomException("用户未登录");
        }
        //设置当前登录的用户id
        shoppingCart.setUserId(currentId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        //判断当前加入购物车的是菜品还是套餐
        if (dishId != null) {
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
//            queryWrapper.eq(ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
        } else if (setmealId != null) {
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        } else {
            throw new CustomException("未知异常");
        }
        ShoppingCart shop = shoppingCartService.getOne(queryWrapper);

        //判断是否为第二次添加购物车
        if (shop != null) {
            //第二次添加
            shop.setNumber(shop.getNumber() + 1);
            shoppingCartService.updateById(shop);
        } else {
            // 第一次添加，直接加入购物车
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shop = shoppingCart;
        }
        return BaseResponse.success(shop);
    }

    @PostMapping("/sub")
    public BaseResponse<ShoppingCart> sub(@RequestBody Map<String, Long> map) {
        //根据用户id和菜品/套餐id，将number-1
        Long setmealId = map.get("setmealId");
        Long dishId = map.get("dishId");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if (dishId != null) {
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else if (setmealId != null) {
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        } else {
            throw new CustomException("未知异常");
        }
        ShoppingCart shoppingCart = shoppingCartService.getOne(queryWrapper);
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        if (shoppingCart.getNumber() <= 0){
            shoppingCartService.removeById(shoppingCart);
        }else {
            shoppingCartService.updateById(shoppingCart);
        }
        return BaseResponse.success(shoppingCart);
    }

    @DeleteMapping("/clean")
    public BaseResponse<String> clean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return BaseResponse.success("购物车以清空");
    }
}
